package com.nuanshui.heatedloan.task.proxy;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nuanshui.framework.utils.LogUtil;

public abstract  class AbstractMessageListenerBeanClassFactory implements MessageListenerBeanClassFactory {

	private Logger log = LoggerFactory.getLogger(AbstractMessageListenerBeanClassFactory.class);
	private HashMap<String,MetaClass> existMetaClass = new HashMap<String,MetaClass>();
	
	protected boolean isCover = false;
	
	/**
	 *  动态生成一个类名为className 的 class
	 * @param @param className
	 * @param @return
	 * @author guohao
	 * @date 2017年2月13日 上午9:57:37
	 * @since $
	 *  
	 * @see com.nuanshui.heatedloan.task.proxy.MessageListenerBeanClassFactory#createProxyClass(java.lang.String)
	 *
	 */
	@Override
	public MetaClass createProxyClass(String className) {
		MetaClass  metaClass = null;
		if(existMetaClass.containsKey(className)) {
			LogUtil.warn(log, className+"  已经存在");
			if(isCover) {//表示新的覆盖原来的旧的
				LogUtil.warn(log, "创建新的代理类");
				try {
					metaClass = doCreateProxyClass(className);	
				} catch (Exception e) {
					LogUtil.error(log, "动态创建"+className+" 失败", e);
				}
			}else {
				metaClass =  existMetaClass.get(className);
			}
		}else {
			try {
				metaClass = doCreateProxyClass(className);	
			} catch (Exception e) {
				LogUtil.error(log, "动态创建"+className+" 失败", e);
			}	
		}	
		return metaClass;
	}
	
	public abstract MetaClass doCreateProxyClass(String className);

}
